import { useEffect } from 'react'

let observer: IntersectionObserver
const useObserverHook = (ele: string, callback: (v: any) => void, watch: any = []) => {
    useEffect(() => {
        const dom = document.querySelector(ele)
        if (dom) {
            observer = new IntersectionObserver((entries) => {
                callback?.(entries)
            })
            observer.observe(dom)
        }
        return () => {
            if (dom && observer) {
                observer.unobserve(dom)
                observer.disconnect()
            }
        }
    }, watch)
}

export default useObserverHook